<?php
namespace app\api\controller;

use app\common\controller\Init;
use think\Controller;
use think\Db;
use think\Request;
use \app\api\model\Option;
use \app\api\model\User;
use \app\api\model\UploadHandler;
use app\api\model\FileManage;
use think\Session;

class Upload extends Init {

	public function index(){
		ob_end_clean();
		$file = request()->file('file');
		$fileInfo = Request::instance()->request();
		$UploadHandler = new UploadHandler($this->userObj->groupData['policy_name'],$this->UID);
		return $UploadHandler->fileReceive($file,$fileInfo);
	}

    /**
     * 获取用户的上传配置
     */
    public function conf() {
        $data = [
            /*'max' => 1,
            'max_upload' => 1,
            'max_upload_size' => 1024,
            'upload_url' => 'http://pan.zcyso.cc/api/upload',
            'chunkSize' => 1*1024*1024,
            'max_file_size' => 1*1024*1024,
            'zone_size' => 1*1024*1024*/
            'max' => 1,
            'max_upload' => 1,
            'max_upload_size' => 0,
            'upload_url' => 'http://disk.zcyso.cc/upload/block',
            'chunkSize' => 4194304, // 512 kb
            'max_file_size' => 0,
            'zone_size' => 1*1024*1024*1024*2
        ];

        success($data);
    }

    public function checkFile() {
        $md5 = input('param.identifier');
        $fileSize = input('param.totalSize');
        $fileName = input('param.filename');
        $relativePath = input('param.relativePath');
        $save_path = input('param.save_path');
        $pdir = input('param.pdir');



        $UploadHandler = new UploadHandler($this->userObj->groupData['policy_name'],$this->UID);
        if($UploadHandler->sotrageCheck($this->UID, $fileSize) == false) {
            error('空间容量不足',411);
        }

        $DirId = FileManage::folderPath2Id($pdir, $save_path, $this->UID);

        // 判断数据库中是否有文件,有文件则秒传
        $File = $UploadHandler->getFileByMd5($md5);
        if($File) {
            // 秒传
            success(['skipUpload' => true], 20001);
            return $UploadHandler->secondUpload($File, $fileName, $DirId);
        }

        return success(['uptoken' => $this->Token()]);
    }
	
	public function Token(){
		$uploadObj = new UploadHandler($this->userObj->groupData['policy_name'],$this->userObj->uid);
		$upToken = $uploadObj->getToken();
		if(!empty($uploadObj->upyunPolicy)){
			return json([
				"token" => $upToken,
				"policy" => $uploadObj->upyunPolicy,
				]);
		}
		if(!empty($uploadObj->s3Policy)){
			return json([
				"policy" => $uploadObj->s3Policy,
				"sign" =>  $uploadObj->s3Sign,
				"key" => $uploadObj->dirName,
				"credential" => $uploadObj->s3Credential,
				"x_amz_date" => $uploadObj->x_amz_date,
				"siteUrl"=>$uploadObj->siteUrl,
				"callBackKey" => $uploadObj->callBackKey,
				]);
		}
		if(!$uploadObj->getToken()){
			return json([
				"uptoken" => $uploadObj->ossToken,
				"sign" => $uploadObj->ossSign,
				"id" => $uploadObj->ossAccessId,
				"key" => $uploadObj->ossFileName,
				"callback" => $uploadObj->ossCallBack,
				]);
		}
		return $uploadObj->getToken();
	}

	public function chunk(){
		$file = file_get_contents('php://input');
		$uploadObj = new UploadHandler($this->userObj->groupData['policy_name'],$this->userObj->uid);
		$uploadObj->setChunk(input('param.'),$file);
	}

	public function mkFile(){
		$ctx = file_get_contents('php://input');
		$originName = UploadHandler::b64Decode(input('param.fname'));
		$filePath = UploadHandler::b64Decode(input('param.path'));
		$uploadObj = new UploadHandler($this->userObj->groupData['policy_name'],$this->userObj->uid);
		$uploadObj->generateFile($ctx,$originName,$filePath);
	}

}
